﻿Imports System.Xml

Public Class Descriptor
	Public Sub New()
		ListItems = New List(Of ListItem)
	End Sub

	Public Property ListItems As List(Of ListItem)

	Public Shared Function ReadXml(reader As XmlReader) As Descriptor
		Dim result As New Descriptor

		While reader.Read()
			If reader.Name = "ListItem" Then
				Select Case reader.GetAttribute("type")
					Case "string"
						result.ListItems.Add(New ListItemString(reader.ReadElementContentAsString()))
					Case "enumeration"
						result.ListItems.Add(New ListItemEnum(Parser.ParseEnum(Of PageNumberStyle)(reader.ReadElementContentAsString())))
					Case "bool"
						result.ListItems.Add(New ListItemBool(reader.ReadElementContentAsBoolean()))
					Case "long"
						result.ListItems.Add(New ListItemLong(reader.ReadElementContentAsInt()))
				End Select
			ElseIf reader.Name = "Descriptor" And reader.NodeType = XmlNodeType.EndElement Then
				Exit While
			End If
		End While

		Return result
	End Function
End Class

Public Class ListItem
End Class

Public Class ListItemString
	Inherits ListItem

	Sub New(value As String)
		Me.Value = value
	End Sub

	Public Property Value As String
End Class

Public Class ListItemEnum
	Inherits ListItem

	Public Sub New(value As PageNumberStyle)
		Me.Value = value
	End Sub

	Public Property Value As PageNumberStyle
End Class

Public Class ListItemBool
	Inherits ListItem

	Sub New(value As Boolean)
		Me.Value = value
	End Sub

	Public Property Value As Boolean
End Class

Public Class ListItemLong
	Inherits ListItem

	Private _value As Integer

	Sub New(value As Integer)
		Me.Value = value
	End Sub

	Public Property Value As Integer
		Get
			Return _value
		End Get
		Set(value As Integer)
			If value >= 1 And value <= 999999 Then
				_value = value
			End If
		End Set
	End Property
End Class

Public Class ListItemUnit
	Inherits ListItem

	Private _value As Double

	Public Sub New(value As Double)
		Me.Value = value
	End Sub

	Public Property Value() As Double
		Get
			Return _value
		End Get
		Set(value As Double)
			If value >= 1 And value <= 8640 Then
				_value = value
			End If
		End Set
	End Property
End Class

Public Class ListItemDouble
	Inherits ListItem

	Private _value As Double

	Public Sub New(value As Double)
		Me.Value = value
	End Sub

	Public Property Value() As Double
		Get
			Return _value
		End Get
		Set(value As Double)
			If value >= 0 And value <= 255 Then
				_value = value
			End If
		End Set
	End Property
End Class